#include "GameWindow.h"
#include "obstacle.h"
#include <QKeyEvent>
#include <QGraphicsTextItem>
#include <QFont>
#include <QBrush>
#include <QImage>

GameWindow::GameWindow(QWidget *parent)
    : QGraphicsView(parent), gameSpeed(5), gameOver(false)
{
    setFixedSize(800, 400);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    scene = new QGraphicsScene(this);
    scene->setSceneRect(0, 0, 800, 400);
    setScene(scene);

    // 设置背景
    QImage bgImage(":/images/D:/background.jpg");
    scene->setBackgroundBrush(QBrush(bgImage.scaled(800, 400)));

    initPlayer();
    initTimers();
}

GameWindow::~GameWindow()
{
    delete gameTimer;
    delete spawnTimer;
    delete elapsedTimer;
}

void GameWindow::initPlayer()
{
    player = new Player();
    scene->addItem(player);
    player->setFlag(QGraphicsItem::ItemIsFocusable);
    player->setFocus();
}

void GameWindow::initTimers()
{
    gameTimer = new QTimer(this);
    connect(gameTimer, &QTimer::timeout, this, &GameWindow::update);
    gameTimer->start(16); // ~60 FPS

    spawnTimer = new QTimer(this);
    connect(spawnTimer, &QTimer::timeout, this, &GameWindow::spawnObstacle);
    spawnTimer->start(1500); // 每1.5秒生成障碍物

    elapsedTimer = new QElapsedTimer();
    elapsedTimer->start();
}

void GameWindow::update()
{
    if(gameOver) return;

    player->update();

    // 更新所有障碍物
    QList<QGraphicsItem*> items = scene->items();
    for(QGraphicsItem *item : items) {
        GameObject *obj = dynamic_cast<GameObject*>(item);
        if(obj && obj != player) {
            obj->update();
        }
    }

    // 每10秒增加游戏速度
    if(elapsedTimer->elapsed() > 10000) {
        gameSpeed += 1;
        elapsedTimer->restart();
    }

    checkCollisions();
}

void GameWindow::spawnObstacle()
{
    if(gameOver) return;

    Obstacle *obstacle = new Obstacle(gameSpeed);
    scene->addItem(obstacle);
}

void GameWindow::checkCollisions()
{
    QList<QGraphicsItem*> colliding = player->collidingItems();
    for(QGraphicsItem *item : colliding) {
        if(dynamic_cast<Obstacle*>(item)) {
            gameOver = true;
            gameOverScreen();
            break;
        }
    }
}

void GameWindow::gameOverScreen()
{
    gameTimer->stop();
    spawnTimer->stop();

    QGraphicsTextItem *gameOverText = new QGraphicsTextItem("Game Over");
    QFont font("Arial", 30, QFont::Bold);
    gameOverText->setFont(font);
    gameOverText->setDefaultTextColor(Qt::red);
    int xPos = width()/2 - gameOverText->boundingRect().width()/2;
    int yPos = height()/2 - gameOverText->boundingRect().height()/2;
    gameOverText->setPos(xPos, yPos);
    scene->addItem(gameOverText);

    QGraphicsTextItem *restartText = new QGraphicsTextItem("Press R to Restart");
    restartText->setFont(QFont("Arial", 20));
    restartText->setDefaultTextColor(Qt::white);
    xPos = width()/2 - restartText->boundingRect().width()/2;
    yPos = height()/2 + 50;
    restartText->setPos(xPos, yPos);
    scene->addItem(restartText);
}

void GameWindow::keyPressEvent(QKeyEvent *event)
{
    if(gameOver && event->key() == Qt::Key_R) {
        // 重新开始游戏
        scene->clear();
        gameOver = false;
        gameSpeed = 5;
        initPlayer();
        initTimers();
    } else {
        QGraphicsView::keyPressEvent(event);
    }
}
